package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Saler;

import dao.DaoHelper;
import dao.SalerDao;

public class SalerDaoImp implements SalerDao {

	private static SalerDaoImp salerDao = new SalerDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private SalerDaoImp(){}

	public static SalerDaoImp getInstance()
	{
		return salerDao;
	}

	@Override
	public boolean save(Saler saler) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("insert into saler value(?, ?, ?, ?, ?)");
			stmt.setString(1, saler.getName());
			stmt.setString(2, saler.getPassword());
			stmt.setString(3, saler.getGender());
			stmt.setInt(4, saler.getAge());
			stmt.setString(5, saler.getShop());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}

	@Override
	public Saler find(String name, String password) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		String g = null;
		int a = 0;
		String s = null;
		Saler saler = null;

		try {	
			stmt=con.prepareStatement("select * from saler where name = ? and password = ?");
			stmt.setString(1, name);
			stmt.setString(2, password);
			result=stmt.executeQuery();
			if (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				g = result.getString(3);
				a = result.getInt(4);
				s = result.getString(5);
				saler = new Saler(n, p, g, a, s);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return saler;
		
	}

	@Override
	public boolean updateByName(Saler saler) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update saler set password = ?, gender = ?, age = ?, shop = ? where name = ?");
			stmt.setString(1, saler.getPassword());
			stmt.setString(2, saler.getGender());
			stmt.setInt(3, saler.getAge());
			stmt.setString(4, saler.getShop());
			stmt.setString(5, saler.getName());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}

	@Override
	public List<Saler> findAll() {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		String g = null;
		int a = 0;
		String s = null;
		Saler saler = null;
		ArrayList<Saler> list = new ArrayList<Saler>();

		try {	
			stmt=con.prepareStatement("select * from saler");
			result=stmt.executeQuery();
			while (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				g = result.getString(3);
				a = result.getInt(4);
				s = result.getString(5);
				saler = new Saler(n, p, g, a, s);
				list.add(saler);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return list;
		
	}

	@Override
	public Saler find(String name) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		String g = null;
		int a = 0;
		String s = null;
		Saler saler = null;

		try {	
			stmt=con.prepareStatement("select * from saler where name = ?");
			stmt.setString(1, name);
			result=stmt.executeQuery();
			if (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				g = result.getString(3);
				a = result.getInt(4);
				s = result.getString(5);
				saler = new Saler(n, p, g, a, s);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return saler;
		
	}

	@Override
	public boolean removeByName(String name) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("delete from saler where name = ?");
			stmt.setString(1, name);
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}
		
		return success;
		
	}

}
